package cn.shiwei.org.service.impl;

import cn.shiwei.basic.service.impl.BaseServiceImpl;
import cn.shiwei.org.domain.Department;
import cn.shiwei.org.mapper.DepartmentMapper;
import cn.shiwei.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 1363732197
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department); // 返回主键
        update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        Department parentDep = department.getParentDep();
        if(parentDep == null || parentDep.getId() == null){ // 顶级部门
            department.setDirPath("/" + (department.getId()));
        }else {
            Department parent = departmentMapper.loadById(department.getParentDep().getId());
            String pdirPath = parent.getDirPath();
            department.setDirPath(pdirPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
